﻿using System;
using System.Collections;
using System.Drawing;
using System.Windows.Forms;
using System.Xml;

namespace Ellanet
{
    public partial class DynamicParameters : Form // KryptonForm
    {
        private const int controlHeight = 24;
        private const int controlWidth = 290;
        private const int controlSpacing = 12;
        private const int controlLeft = 152;
        private const int labelLeft = 12;
        private const int startingFormHeight = 80;

        private int lastControlBottom = 0;
        private int controlCount = 0;
        private ToolTip tt = null;
        private string _ArgumentString = null;

        public string ArgumentString
        {
            get
            {
                return _ArgumentString;
            }
        }

        public DynamicParameters(string commandName)
        {
            InitializeComponent();
            this.Text = String.Format("{0} Parameters", commandName);
            okButton.Click += new EventHandler(okButton_Click);
        }

        void okButton_Click(object sender, EventArgs e)
        {
            _ArgumentString = BuildArgumentString();
            this.DialogResult = DialogResult.OK;
            this.Close();
        }

        private string BuildArgumentString()
        {
            string arguments = null;

            foreach (Control c in this.Controls)
            {
                switch (c.GetType().ToString())
                {
                    case "System.Windows.Forms.ComboBox":
                        arguments += String.Format("{0} ", ((ComboBox)c).Text);
                        break;
                    case "System.Windows.Forms.TextBox":

                        if ((((TextBox)c).Text != null) && (((TextBox)c).Text.Trim().Length > 0))
                        {
                            arguments += String.Format("\"{0}\" ", ((TextBox)c).Text);
                        }

                        break;
                    case "System.Windows.Forms.CheckBox":

                        if (((CheckBox)c).Checked)
                        {
                            arguments += String.Format("{0} ", ((CheckBox)c).Text);
                        }

                        break;
                }
            }

            if (arguments != null)
            {
                arguments = arguments.Trim();
            }

            return arguments;
        }

        public void AddBooleanParameter(string name, string hint)
        {
            CheckBox cb = new CheckBox();
            cb.Text = name;
            cb.TextAlign = ContentAlignment.MiddleLeft;
            ConfigureControl(cb, name, hint, false);
        }

        public void AddTextParameter(string name, string hint)
        {
            TextBox tb = new TextBox();
            ConfigureControl(tb, name, hint, true);
        }

        public void AddListParameter(string name, ArrayList list, string hint)
        {
            ComboBox cb = new ComboBox();
            cb.DropDownStyle = System.Windows.Forms.ComboBoxStyle.DropDownList;

            foreach (string item in list)
            {
                cb.Items.Add(item);
            }

            cb.SelectedIndex = 0;
            ConfigureControl(cb, name, hint, true);
        }

        private void ConfigureControl(Control c, string commandName, string commandHint, bool showLabel)
        {
            controlCount += 1;

            if (showLabel)
            {
                Label label = new Label();
                label.Location = new Point(labelLeft, (lastControlBottom + controlSpacing));
                label.Size = new Size(0, controlHeight);
                label.AutoSize = true;
                label.Text = String.Format("{0}:", commandName);
                label.TextAlign = ContentAlignment.MiddleLeft;
                label.Name = String.Format("dynamicLabel{0}", controlCount.ToString());
                label.ForeColor = System.Drawing.Color.Black;
                label.Visible = true;
                this.Controls.Add(label);
            }

            c.Location = new Point(controlLeft, (lastControlBottom + controlSpacing));
            c.Size = new Size(controlWidth, controlHeight);
            c.Name = String.Format("dynamicControl{0}", controlCount.ToString());
            c.Font = new Font("Consolas", c.Font.Size);
            c.ForeColor = System.Drawing.Color.Blue;
            c.TabIndex = (controlCount - 1);
            XmlDocument tagXmlDoc = new XmlDocument();
            tagXmlDoc.LoadXml("<command><name /><hint /></command>");
            tagXmlDoc.SelectSingleNode("command/name").InnerText = commandName;
            tagXmlDoc.SelectSingleNode("command/hint").InnerText = commandHint;
            c.Tag = tagXmlDoc.InnerXml;
            c.MouseHover += new EventHandler(c_MouseHover);
            c.Visible = true;
            lastControlBottom = c.Bottom;
            this.Controls.Add(c);
            this.Height = (startingFormHeight + lastControlBottom);
        }

        void c_MouseHover(object sender, EventArgs e)
        {
            if (tt != null)
            {
                tt.Dispose();
            }

            XmlDocument tagXmlDoc = new XmlDocument();
            tagXmlDoc.LoadXml(((Control)sender).Tag.ToString());
            tt = new ToolTip();
            tt.RemoveAll();
            tt.ToolTipTitle = tagXmlDoc.SelectSingleNode("command/name").InnerText;
            tt.ToolTipIcon = ToolTipIcon.Info;
            tt.UseAnimation = true;
            tt.UseFading = true;
            tt.SetToolTip((Control)sender, tagXmlDoc.SelectSingleNode("command/hint").InnerText);
        }
    }
}
